<?php
/**
 * @version 1.0
 * @since Aug-2008
 * @package lib
 * @subpackage vimua
 * @name CUploadProduct
 * @deprecated Lớp thao tác với ảnh đã upload
*/
require_once '3rdparty/Asido/class.asido.php';
require_once 'lib/utils/CFtpUpload.php';
require_once 'lib/utils/CImage.php';
class CFtpUploadImage extends CFtpUpload{
	function __construct($dir_name, $input_name, $prefix)
	{
		if (extension_loaded('imagick')){
			asido::driver('imagick_ext');
		}else {
			asido::driver('gd');
		}
		parent::__construct($dir_name, $input_name, $prefix);
	}

	function upload_product_image(&$thumbnails){
		$image_sizes = array(
				'small'=>100,
				'medium'=>200,
				'large'=>300
				);
		$list_images = $this->upload(IMAGE_EXTENSION);
		$portal_id = portal()->id;
		if(!empty($list_images)){
			CFile('upload/'.$portal_id.'/product/image/'.date('Ymd').'/')->mkdir();
			$image_dir['large'] = CFile('upload/'.$portal_id.'/product/large/'.date('Ymd').'/')->mkdir();
			$image_dir['medium'] = CFile('upload/'.$portal_id.'/product/medium/'.date('Ymd').'/')->mkdir();
			$image_dir['small'] = CFile('upload/'.$portal_id.'/product/small/'.date('Ymd').'/')->mkdir();
			CFile(FTP_PREFIX.'upload/'.$portal_id.'/product/large/'.date('Ymd').'/')->mkdir();
			CFile(FTP_PREFIX.'upload/'.$portal_id.'/product/medium/'.date('Ymd').'/')->mkdir();
			CFile(FTP_PREFIX.'upload/'.$portal_id.'/product/small/'.date('Ymd').'/')->mkdir();
			foreach ($list_images as $key1=>$val) {
				foreach ($image_sizes as $key2=>$size){
					$i = Asido::Image($val, $image_dir[$key2].$key1);
					Asido::Frame($i, $size, $size);
					$i->Save(ASIDO_OVERWRITE_ENABLED);
					$thumbnails[$key2][$key1] = $image_dir[$key2].$key1;
					$fp = fopen($image_dir[$key2].$key1, 'r');
					$result = ftp_fput(CFile::ftp_connection_id(), $image_dir[$key2].$key1, $fp, FTP_BINARY);
					fclose($fp);
				}
			}
			return $list_images;
		} else {
			return false;
		}
	}

	function upload_category_image(&$thumbnails){
		$list_images = $this->upload(IMAGE_EXTENSION);
		$image_dir = CFile('upload/'.portal()->id.'/category/image/'.date('Ymd').'/')->mkdir();
		$thumb_dir = CFile('upload/'.portal()->id.'/category/thumbnail/'.date('Ymd').'/')->mkdir();
		CFile(FTP_PREFIX.'upload/'.portal()->id.'/category/image/'.date('Ymd').'/')->mkdir();
		CFile(FTP_PREFIX.'upload/'.portal()->id.'/category/thumbnail/'.date('Ymd').'/')->mkdir();
		foreach ($list_images as $key=>$val) {
			$i = Asido::Image($val, $thumb_dir.$key);
			Asido::Frame($i, 132, 116);
			$i->Save(ASIDO_OVERWRITE_ENABLED);
			$thumbnails[$key] = $thumb_dir.$key;
			$fp = fopen($thumb_dir.$key, 'r');
			$result = ftp_fput(CFile::ftp_connection_id(), $thumb_dir.$key, $fp, FTP_BINARY);
			fclose($fp);
		}
		return $list_images;
	}

	function upload_avatar_image(){
		$list_images = $this->upload(IMAGE_EXTENSION);
		if(!empty($list_images)){
			$errors = array();
			foreach ($list_images as $key=>$val) {
				if(CImage::check_image($val)){
					$i = Asido::Image($val, $val);
					Asido::Frame($i, 80, 80);
					$i->Save(ASIDO_OVERWRITE_ENABLED);
					$fp = fopen($val, 'r');
					$result = ftp_fput(CFile::ftp_connection_id(), $val, $fp, FTP_BINARY);
					fclose($fp);
				}else {
					$errors[] = 'IMAGE_HEADER_ERROR';
				}
			}
			if(empty($errors)){
				return $list_images;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}

	function upload_image_by_sizes($image_sizes=false, $resize_config=false, $resize_sizes=false, $color=null){
		$list_images = $this->upload(IMAGE_EXTENSION);
		if(!empty($list_images)){
			if(!empty($resize_config) && !empty($resize_sizes)){
				switch ($resize_config){
					case 'width':
						$this->resize_by_width($list_images, $resize_sizes);
						break;
					case 'height':
						$this->resize_by_height($list_images, $resize_sizes);
						break;
				}
			}
			if(!empty($image_sizes)){
				foreach ($list_images as $key1=>$val1) {
					foreach ($image_sizes as $key2=>$size){
						$size_dir = CFile('upload/'.portal()->id.'/'.$key2.'/'.date('Ymd').'/')->mkdir();
						CFile(FTP_PREFIX.'upload/'.portal()->id.'/'.$key2.date('Ymd').'/')->mkdir();
						$i = Asido::Image($val1, $size_dir.$key1);
						if(isset($color[$key2])){
							$obj_color = new Asido_Color;
							$obj_color->set(intval($color[$key2]['red']),intval($color[$key2]['green']),intval($color[$key2]['blue']));
						}else {
							$obj_color = null;
						}
						Asido::Frame($i, intval($size), intval($size),$obj_color);
						$i->Save(ASIDO_OVERWRITE_ENABLED);
						$fp = fopen($val1, 'r');
						$result = ftp_fput(CFile::ftp_connection_id(), $val1, $fp, FTP_BINARY);
						fclose($fp);
					}
				}
			}
			return $list_images;
		}else {
			return false;
		}
	}

	function resize_by_width($list_images, $image_sizes){
		$list_images = $this->upload(IMAGE_EXTENSION);
		if(!empty($list_images) && !empty($image_sizes)){
			foreach ($list_images as $key1=>$val1) {
				foreach ($image_sizes as $key2=>$size){
					$size_dir = CFile('upload/'.portal()->id.'/'.$key2.'/'.date('Ymd').'/')->mkdir();
					CFile(FTP_PREFIX.'upload/'.portal()->id.'/'.$key2.date('Ymd').'/')->mkdir();
					$i = Asido::Image($val1, $size_dir.$key1);
					Asido::width($i, intval($size));
					$i->Save(ASIDO_OVERWRITE_ENABLED);
					$fp = fopen($val1, 'r');
					$result = ftp_fput(CFile::ftp_connection_id(), $val1, $fp, FTP_BINARY);
					fclose($fp);
				}
			}
		}
	}

	function resize_by_height($list_images, $image_sizes){
		if(!empty($list_images) && !empty($image_sizes)){
			foreach ($list_images as $key1=>$val1) {
				foreach ($image_sizes as $key2=>$size){
					$size_dir = CFile('upload/'.portal()->id.'/'.$key2.'/'.date('Ymd').'/')->mkdir();
					CFile(FTP_PREFIX.'upload/'.portal()->id.'/'.$key2.date('Ymd').'/')->mkdir();
					$i = Asido::Image($val1, $size_dir.$key1);
					Asido::height($i, intval($size));
					$i->Save(ASIDO_OVERWRITE_ENABLED);
					$fp = fopen($val1, 'r');
					$result = ftp_fput(CFile::ftp_connection_id(), $val1, $fp, FTP_BINARY);
					fclose($fp);
				}
			}
		}
	}
}
function CFtpUploadImage($dir_name, $input_name, $prefix){
	return new CFtpUploadImage($dir_name, $input_name, $prefix);
}
?>